﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace MS.Utility.Files
{
    /// <summary>
    /// 目录功能类
    /// </summary>
    public class DirectoryUtility
    {
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="path">需要创建的文件夹绝对路径</param>
        public static void CreateDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        /// <summary>
        /// 根据文件名称创建文件夹
        /// </summary>
        /// <param name="fileName">文件名称</param>
        public static void CreateDirectoryFromFileName(string fileName)
        {
            string path = System.IO.Path.GetDirectoryName(fileName);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        /// <summary>
        /// 复制文件夹中的子目录、文件到指定文件夹
        /// （保留第一个文件夹目录，覆盖文件）
        /// </summary>
        /// <param name="sourceDir">源文件夹绝对路径</param>
        /// <param name="destDir">目标文件夹绝对路径</param>
        public static void CopyDirectory(string sourceDir, string destDir)
        {
            CopyDirectory(sourceDir, destDir, true, true);
        }


        /// <summary>
        /// 复制文件夹中的子目录、文件到指定文件夹
        /// </summary>
        /// <param name="sourceDir">源文件夹路径</param>
        /// <param name="destDir">保存路径</param>
        /// <param name="keepFirstDir">是否保留第一个文件夹目录</param>
        /// <param name="overwrite">是否覆盖已有文件</param>
        public static void CopyDirectory(string sourceDir, string destDir, bool keepFirstDir, bool overwrite)
        {
            if (sourceDir.EndsWith("\\"))
            {
                sourceDir = sourceDir.TrimEnd(new char[] { '\\' });
            }
            int dirPos = sourceDir.LastIndexOf("\\") + 1;
            string dirName = sourceDir.Substring(dirPos);//获取文件夹名
            DirectoryInfo DirectoryArray = new DirectoryInfo(sourceDir);
            FileInfo[] Files = DirectoryArray.GetFiles();//获取该文件夹下的文件列表     
            DirectoryInfo[] Directorys = DirectoryArray.GetDirectories();//获取该文件夹下的文件夹列表 

            if (keepFirstDir)
            {
                destDir += "\\" + dirName;
            }

            if (!Directory.Exists(destDir))
            {
                Directory.CreateDirectory(destDir);//文件夹不存在，创建     
            }
            foreach (FileInfo inf in Files)//逐个复制文件     
            {
                if (overwrite)
                {
                    File.Copy(sourceDir + "\\" + inf.Name, destDir + "\\" + inf.Name, overwrite);
                }
                else
                {
                    if (!File.Exists(destDir + "\\" + inf.Name))
                        File.Copy(sourceDir + "\\" + inf.Name, destDir + "\\" + inf.Name, overwrite);
                }
            }
            foreach (DirectoryInfo Dir in Directorys)//逐个获取文件夹名称，并递归调用方法本身     
            {
                CopyDirectory(sourceDir + "\\" + Dir.Name, destDir, true, overwrite);
            }
        }


        /// <summary>
        /// 提供绝对路径，删除目录
        /// </summary>
        /// <param name="path">需要删除的目录</param>
        public static void DeleteDirectory(String path)
        {
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }
        }

    }
}

